//
//  YDMyCardTableViewCell.m
//  IngCard
//
//  Created by kf on 2017/8/13.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDMyCardTableViewCell.h"


@interface MyCollectionViewCell : UICollectionViewCell

@property (strong, nonatomic) UIImageView *headerImageView;

@property (strong, nonatomic) UILabel *nameLabel;

@end
//60 *70 
@implementation MyCollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _headerImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];
        [self.contentView addSubview:_headerImageView];
        
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 57, 66, 13)];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.textColor = UIColorMake(113, 108, 108);
        _nameLabel.font = UIMediumFontMake(9);
        [self.contentView addSubview:_nameLabel];
    }
    return self;
}
@end




@implementation YDMyCardCellDataSource

-(Class) viewClass
{
    return [YDMyCardTableViewCell class];
}

@end

@interface YDMyCardTableViewCell()<UICollectionViewDataSource,
                                    UICollectionViewDelegate,
                                    UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) UIImageView *typeImageView;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UICollectionView *contactsCollectionView;
@property(nonatomic, strong) UILabel *createtimeLabel;

@property(nonatomic, strong) UIImageView *editImageView;
@property(nonatomic, strong) UIButton *editButton;

//contentView
@property(nonatomic, strong) UIImageView *editingImageView;
@property(nonatomic, strong) UILabel *contentLabel;
@property(nonatomic, strong) UIImageView *contentBackground;




@end

@implementation YDMyCardTableViewCell

+(CGFloat)viewHeight:(YDMyCardCellDataSource *) dataSource
{
    return dataSource.height ?:  190;
}


- (void) initSubviews
{
    [super initSubviews];
    
    self.contactsCollectionView.dataSource = self;
    self.contactsCollectionView.delegate = self;
    
    [self.contentView addSubview:self.typeImageView];
    [self.typeImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(15);
        make.top.equalTo(20);
        make.width.equalTo(28);
        make.height.equalTo(28);
    }];
    
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeImageView.mas_right).offset(10);
        make.centerY.equalTo(self.typeImageView);
    }];
    
    [self.contentView addSubview:self.createtimeLabel];
    [self.createtimeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(27);
        make.bottom.equalTo(self.contentView).offset(-11);
    }];
    
    [self.contentView addSubview:self.editButton];
    [self.editButton makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-20);
        make.centerY.equalTo(self.createtimeLabel);
    }];
    
    [self.contentView addSubview:self.editImageView];
    [self.editImageView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.editButton.mas_left).offset(-1);
        make.centerY.equalTo(self.createtimeLabel);
    }];
    
    [self.contentView addSubview:self.contactsCollectionView];
    [self.contactsCollectionView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(22);
        make.top.equalTo(self.typeImageView.mas_bottom).offset(26);
        make.height.equalTo(75);
        make.right.equalTo(self.contentView);
    }];
}

-(void) onViewModelChanged
{
    YDMyCardCellDataSource *dataSource = (YDMyCardCellDataSource *)self.dataSource;
    if (dataSource)
    {
        if (dataSource.activity.type == YDActivity_card)
        {
            self.typeImageView.image = UIImageMake(@"myCard_activity_card");
            self.titleLabel.text = NSStringFormat(@"您收到了%lu张名片",(unsigned long)dataSource.activity.contacts.count);
        }
        else
        {
            self.typeImageView.image = UIImageMake(@"myCard_activity_contact");
            self.titleLabel.text = NSStringFormat(@"您添加了%lu位联系人",(unsigned long)dataSource.activity.contacts.count);
        }
        [self.titleLabel sizeToFit];
        
    
        self.createtimeLabel.text = dataSource.activity.createTime;
        
        if (dataSource.activity.content && (dataSource.activity.content.length > 0))
        {
            self.editImageView.hidden = YES;
            self.editButton.hidden = YES;
            
            NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
            // 对齐方式
            style.alignment = NSTextAlignmentJustified;
            // 首行缩进
            style.firstLineHeadIndent = 15.0f;
            // 头部缩进
            style.headIndent = 15.0f;
            // 尾部缩进
            style.tailIndent = -10.0f;
            NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:dataSource.activity.content attributes:@{ NSParagraphStyleAttributeName : style}];
            self.contentLabel.attributedText = attrText;
            
            
            CGSize size = CGSizeMake(SCREEN_WIDTH - 70, MAXFLOAT);
            
            NSMutableParagraphStyle *editstyle = [[NSMutableParagraphStyle alloc] init];
            NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading;
            [editstyle setLineBreakMode:NSLineBreakByWordWrapping];
            CGRect frame = [dataSource.activity.content boundingRectWithSize:size
                                                            options:options
                                                         attributes:@{NSFontAttributeName: UIRegularFontMake(13), NSParagraphStyleAttributeName : editstyle} context:nil];
            
            CGFloat contentHeight = frame.size.height;
            
            dataSource.height = contentHeight + 190 + 35;
            
            [self removeEditView];
            [self addEditView:contentHeight + 35];
        }
        else
        {
            self.editImageView.hidden = NO;
            self.editButton.hidden = NO;
            [self removeEditView];
            dataSource.height = 190;
        }
        [self.contactsCollectionView reloadData];
    }
}


#pragma mark - Private
-(void) edit
{
    YDMyCardCellDataSource *dataSource = (YDMyCardCellDataSource *)self.dataSource;
    if (dataSource)
    {
        BlockGoGo(dataSource.edit);
    }
}

-(void) addEditView:(CGFloat) height
{
    [self.contentView addSubview:self.editingImageView];
    [self.editingImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(27);
        make.top.equalTo(169);
        make.width.equalTo(10);
        make.height.equalTo(10);
    }];
    
    [self.contentView addSubview:self.contentBackground];
    [self.contentBackground makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(25);
        make.right.equalTo(-20);
        make.top.equalTo(170);
        make.height.equalTo(height);
    }];
    
    [self.contentView addSubview:self.contentLabel];
    [self.contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(33);
        make.right.equalTo(-36);
        make.top.equalTo(170);
        make.height.equalTo(height);
    }];
}

-(void) removeEditView
{
    if ([self.contentView.subviews containsObject:self.editingImageView])
    {
        [self.editingImageView removeConstraints:self.editingImageView.constraints];
        [self.editingImageView removeFromSuperview];
    }
    
    if ([self.contentView.subviews containsObject:self.contentBackground])
    {
        [self.contentBackground removeConstraints:self.contentBackground.constraints];
        [self.contentBackground removeFromSuperview];
    }
    
    if ([self.contentView.subviews containsObject:self.contentLabel])
    {
        [self.contentLabel removeConstraints:self.contentLabel.constraints];
        [self.contentLabel removeFromSuperview];
    }
}
#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger count = 0;
    YDMyCardCellDataSource *dataSource = (YDMyCardCellDataSource *)self.dataSource;
    if (dataSource)
    {
        count = dataSource.activity.contacts.count;
    }
    return count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    MyCollectionViewCell *cell = (MyCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
   
    YDMyCardCellDataSource *dataSource = (YDMyCardCellDataSource *)self.dataSource;
    if (dataSource)
    {
        YDContact* contact =  [dataSource.activity.contacts safeObjectAtIndex:indexPath.row];
        
        if (contact)
        {
            UIImageViewSetStr(cell.headerImageView, contact.headImage);
            cell.nameLabel.text = NSStringFormat(@"%@ %@",contact.firstName, contact.lastName);
        }
    }
    return cell;
}

//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(60, 70);
}


//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(1, 1, 1, 1);
}

//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 6.0f;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - lazy loading
-(UIImageView *) typeImageView
{
    if (!_typeImageView)
    {
        _typeImageView = [UIImageView new];
        _typeImageView.image = UIImageMake(@"myCard_activity_card");
        _typeImageView.contentMode = UIViewContentModeCenter;
        [_typeImageView sizeToFit];
    }
    return _typeImageView;
}

-(UILabel *) createtimeLabel
{
    if (!_createtimeLabel)
    {
        _createtimeLabel = [[UILabel alloc] init];
        _createtimeLabel.text = @"2017-06-26，3:30PM";
        _createtimeLabel.textAlignment = NSTextAlignmentLeft;
        _createtimeLabel.font = UIMediumFontMake(10);
        _createtimeLabel.textColor = UIColorMake(155, 155, 155);
        [_createtimeLabel sizeToFit];
    }
    return _createtimeLabel;
}

-(UILabel *) titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"您收到了8张名片";
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = UIMediumFontMake(14);
        _titleLabel.textColor = UIColorMake(82, 82, 82);
        [_titleLabel sizeToFit];
    }
    return _titleLabel;
}



-(UIImageView *) editImageView
{
    if (!_editImageView)
    {
        _editImageView = [UIImageView new];
        _editImageView.image = UIImageMake(@"myCard_activity_edit");
        _editImageView.contentMode = NSTextAlignmentLeft;
        [_editImageView sizeToFit];
    }
    return _editImageView;
}

-(UIButton *) editButton
{
    if (!_editButton)
    {
        _editButton = [[UIButton alloc] init];
        _editButton.frame = CGRectMake(0, 0, 48, 17);
        
        [_editButton setTitle:@"添加描述" forState:(UIControlStateNormal)];
        [_editButton setTitleColor:UIColorMake(151, 151, 151) forState:UIControlStateNormal];
        _editButton.titleLabel.font = UIRegularFontMake(12);
        
        [_editButton addTarget:self
                        action:@selector(edit)
              forControlEvents:UIControlEventTouchUpInside];
    }
    return _editButton;
}

-(UICollectionView *)contactsCollectionView
{
    if (!_contactsCollectionView)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.itemSize =CGSizeMake(60, 70);

        _contactsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH * 2, 70) collectionViewLayout:layout];
        
        _contactsCollectionView.backgroundColor = [UIColor clearColor];
        
        [_contactsCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
        
        _contactsCollectionView.showsHorizontalScrollIndicator = NO;
    }
    return _contactsCollectionView;
}


-(UIImageView *) contentBackground
{
    if (!_contentBackground)
    {
        _contentBackground = [UIImageView new];
        UIImage *image = UIImageMake(@"contact_content_background");
        image = [image  stretchableImageWithLeftCapWidth:image.size.width*0.3 topCapHeight:image.size.height*0.7];
        _contentBackground.image = image;
        _contentBackground.contentMode = UIViewContentModeScaleToFill;
    }
    return _contentBackground;
}

-(UILabel *) contentLabel
{
    if (!_contentLabel)
    {
        _contentLabel = [UILabel new];
        _contentLabel.text = @"Experience";
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = UIRegularFontMake(13);
        _contentLabel.textColor = UIColorMake(21, 26, 65);
        _contentLabel.userInteractionEnabled=YES;
        
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(edit)];
        [_contentLabel addGestureRecognizer:labelTapGestureRecognizer];
    }
    return _contentLabel;
}

-(UIImageView *) editingImageView
{
    if (!_editingImageView)
    {
        _editingImageView = [UIImageView new];
        _editingImageView.image = UIImageMake(@"contact_detail_editing");
        _editingImageView.contentMode = NSTextAlignmentLeft;
        [_editingImageView sizeToFit];
    }
    return _editingImageView;
}

@end
